package com.smh.a18exerciseDemo5;

import java.util.ArrayList;
import java.util.Random;

public class MyThread extends Thread {
    ArrayList<Integer> list;

    public MyThread(ArrayList<Integer> list) {
        this.list = list;
    }

    @Override
    public void run() {

        while (true) {
            synchronized (MyThread.class) {
                if (list.size() == 0) {
                    break;
                } else {

                    Random random = new Random();
                    int index = random.nextInt(list.size());
                    System.out.println(Thread.currentThread().getName() + "又产生了一个" + list.get(index) + "元大奖");
                    list.remove(index);
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
